/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Timer.java
 *
 * Created on Jan 8, 2012, 1:04:08 PM
 */
/**
 *
 * @author Figueroa
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Timer_panel extends javax.swing.JPanel  implements ActionListener{

    /** Creates new form Timer */
    public Timer_panel() {
        initComponents();
       
    }
    public Timer_panel(TrainingPanel entorno){
         initComponents();
         parent = entorno;
    
    }
        TrainingPanel parent;
        Timer timer = new Timer(1000,this); // El timer que se encarga de administrar los tiempo de repeticion
	public int segundos; // manejar el valor del contador
	public boolean frozen; // manejar el estado del contador TIMER AUTOMATICO -- True Detenido | False Corriendo
        
    @Override
    public void actionPerformed(ActionEvent e) {
       segundos --;
        if (segundos >= 0){
            //System.out.println(" ** " + counter);
                       //System.out.println(segundos);
                        int m = segundos /60;
                        int s = segundos % 60;
                   
                        minutestxt.setText(""+m);
                        if(pointstxt.getText().equals(":"))
                            pointstxt.setText("");
                        else
                            pointstxt.setText(":");
                        if(s>=10)
                            secondstxt.setText(""+s);
                        else
                            secondstxt.setText("0"+s);
        }
        if (segundos == 0){
            //System.out.println("Blast off! ");
           timer.stop();
          JOptionPane.showMessageDialog(null, "Se ha acabado el tiempo","TEChess Timer dice:",JOptionPane.INFORMATION_MESSAGE);   
         parent.Timesup();
        }
    
    }
    
 
	public void Start(int pSeg) throws Exception {
	    segundos = pSeg * 60;	
            timer.start();
		
               
                
	}// end Start
 
	public void Stop() {
		System.out.println("Stop");
		timer.stop();
	}// end Stop
 
    public void Reset() {
		timer.restart();
		segundos = 0;
	}// end Reset
    
  
 

    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        minutestxt = new javax.swing.JLabel();
        pointstxt = new javax.swing.JLabel();
        secondstxt = new javax.swing.JLabel();

        setLayout(new java.awt.GridLayout(1, 0));

        minutestxt.setBackground(new java.awt.Color(79, 96, 255));
        minutestxt.setFont(new java.awt.Font("Courier New", 1, 36)); // NOI18N
        minutestxt.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        minutestxt.setText("0");
        minutestxt.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        minutestxt.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        add(minutestxt);

        pointstxt.setBackground(new java.awt.Color(79, 96, 255));
        pointstxt.setFont(new java.awt.Font("Courier New", 1, 36)); // NOI18N
        pointstxt.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pointstxt.setText(":");
        pointstxt.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        add(pointstxt);

        secondstxt.setBackground(new java.awt.Color(79, 96, 255));
        secondstxt.setFont(new java.awt.Font("Courier New", 1, 36)); // NOI18N
        secondstxt.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        secondstxt.setText("00");
        secondstxt.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        add(secondstxt);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel minutestxt;
    private javax.swing.JLabel pointstxt;
    private javax.swing.JLabel secondstxt;
    // End of variables declaration//GEN-END:variables
}
